package entity;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import service.StudentsDAO;
import service.impl.StudentsDAOImpl;

public class TestStudents {
	
	@Test
	public void testSchemaExport(){
		Configuration config = new Configuration().configure(); 
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
		SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.getCurrentSession();
		SchemaExport export= new SchemaExport(config); 
		
		export.create(true, true);
	}
	
	@Test
	public void testSaveStudents(){
		//创建配置对象
		Configuration config = new Configuration().configure();
		//创建服务器注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
		//创建sessionFactory
		SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
		//创建session对象
		Session session = sessionFactory.getCurrentSession();
		//创建事务对象
		Transaction tx = session.beginTransaction();
		
		Students t1 = new Students("s0000001", "张三丰", "男", new Date(), "武当山");
		Students t2 = new Students("s0000002", "郭靖", "男", new Date(), "蒙古草原");
		Students t3 = new Students("s0000003", "黄蓉", "女", new Date(), "桃花岛");
		

		session.save(t1);
		session.save(t2);
		session.save(t3);
		tx.commit();
		sessionFactory.close();
	}
	
	@Test
	public void testQueryAllStudents(){
		StudentsDAO sdao = new StudentsDAOImpl();
		List<Students> list = sdao.queryAllStudetns();
		
		for(int i = 0; i < list.size(); i++){
			System.out.println(list.get(i));
		}
	}
}
